package com.ncu.aldl_server;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import androidx.annotation.Nullable;

import com.ncu.aldl_server.ICat;

import java.util.Timer;
import java.util.TimerTask;

import static android.content.ContentValues.TAG;

/**
 * Created by zhaoliang on 2018/7/27 0027
 */

public class AidlService extends Service {
    private String color;
    private double weight;
    Timer timer = new Timer();
    String[] colors = new String[]{
            "红色", "黄色", "黑色"
    };
    double[] weights = new double[]{
            2.3, 3.1, 1.58
    };
    private CatBinder catBinder;

    @Override
    public void onCreate() {
        super.onCreate();
        catBinder = new CatBinder();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                //随机地改变service组件内color、weight属性值
                int rand = (int) (Math.random() * 3);
                color = colors[rand];
                weight = weights[rand];
                Log.i(TAG, "AIDL_onCreate");
            }
        }, 0, 800);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
    /*
    *1、在绑定本地service的情况下，该catbinder
    对象会直接传给客户端的serviceConnection对象
    的onserviceConnection方法的第二个参数
    /2、在绑定远程service的情况下，该catbinder
    对象会直接传给客户端的serviceConnection对象
    的onserviceConnection方法的第二个参数
    *
    *
    */
        return new CatBinder();
    }
    //继承stub也就是吸纳了ICat接口，并实现了IBinder接口
    public class CatBinder extends ICat.Stub {

        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

        }

        @Override
        public String getColor() throws RemoteException {
            return color;
        }

        @Override
        public double getWeight() throws RemoteException {
            return weight;
        }
    }

    @Override
    public void onDestroy() {
        timer.cancel();
    }
}